// Pipeline Component Library Example
// ----------------------------------
// Colin Bowern (colinbowern@hotmail.com)
// January 2007
//
// Copyright (c) 2007 Colin Bowern
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// * The above copyright notice and this permission notice shall be included in
//   all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#region References
using Microsoft.CommerceServer.Catalog;
using Microsoft.CommerceServer.Runtime;
using Microsoft.CommerceServer.Runtime.Configuration;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
#endregion

namespace RockstarGuys.PipelineComponents
{
    /// <summary>
    /// Strongly typed wrapper for a CommerceResourceCollection
    /// </summary>
    /// <remarks>
    /// For more information see http://msdn2.microsoft.com/en-us/library/microsoft.commerceserver.runtime.configuration.commerceresourcecollection.aspx
    /// </remarks>
    public class CommerceResources
    {
        #region Fields
        private CommerceResourceCollection commerceResourceCollection;
        private string inventorySqlConnectionString;
        private string marketingSqlConnectionString;
        private string transactionConfigSqlConnectionString;
        #endregion

        #region Properties
        /// <summary>
        /// Gets the commerce resource collection.
        /// </summary>
        /// <value>The commerce resource collection.</value>
        public CommerceResourceCollection CommerceResourceCollection
        {
            get { return this.commerceResourceCollection; }
        }

        #region Inventory
        /// <summary>
        /// Gets the inventory resource.
        /// </summary>
        /// <value>The inventory resource.</value>
        public CommerceResource InventoryResource
        {
            get
            {
                CommerceResource commerceResource = CommerceResourceCollection[KeyNames.Inventory.ResourceKey] as CommerceResource;
                return commerceResource;
            }
        }

        /// <summary>
        /// Gets the inventory resource connection string.
        /// </summary>
        /// <remarks>This is an ODBC connection string.</remarks>
        /// <value>The inventory resource connection string.</value>
        public string InventoryConnectionString
        {
            get { return (this.InventoryResource != null ? this.InventoryResource[KeyNames.Inventory.ConnectionString] as string : null);  }
        }

        /// <summary>
        /// Gets the inventory SQL connection string.
        /// </summary>
        /// <value>The inventory SQL connection string.</value>
        public string InventorySqlConnectionString
        {
            get
            {
                if (inventorySqlConnectionString == null)
                {
                    inventorySqlConnectionString = InventoryConnectionString;
                    if (inventorySqlConnectionString != null)
                    {
                        inventorySqlConnectionString = Helper.ConvertOdbcToSqlConnectionString(inventorySqlConnectionString);
                    }
                }
                return inventorySqlConnectionString;
            }
        }
        #endregion

        #region Marketing
        /// <summary>
        /// Gets the marketing resource.
        /// </summary>
        /// <value>The marketing resource.</value>
        public CommerceResource MarketingResource
        {
            get
            {
                CommerceResource commerceResource = CommerceResourceCollection[KeyNames.Marketing.ResourceKey] as CommerceResource;
                return commerceResource;
            }
        }

        /// <summary>
        /// Gets the marketing resource connection string.
        /// </summary>
        /// <remarks>This is an ODBC connection string.</remarks>
        /// <value>The marketing resource connection string.</value>
        public string MarketingConnectionString
        {
            get { return (this.MarketingResource != null ? this.MarketingResource[KeyNames.Marketing.ConnectionString] as string : null); }
        }

        /// <summary>
        /// Gets the default marketing system language.
        /// </summary>
        public string DefaultMarketingSystemLanguage
        {
            get { return (this.MarketingResource != null ? this.MarketingResource[KeyNames.Marketing.DefaultMarketingSystemLanguage] as string : null); }
        }

        /// <summary>
        /// Gets the marketing SQL connection string.
        /// </summary>
        /// <value>The marketing SQL connection string.</value>
        public string MarketingSqlConnectionString
        {
            get
            {
                if (marketingSqlConnectionString == null)
                {
                    marketingSqlConnectionString = MarketingConnectionString;
                    if (marketingSqlConnectionString != null)
                    {
                        marketingSqlConnectionString = Helper.ConvertOdbcToSqlConnectionString(marketingSqlConnectionString);
                    }
                }
                return marketingSqlConnectionString;
            }
        }
        #endregion

        #region Transaction Config
        /// <summary>
        /// Gets the transaction config resource.
        /// </summary>
        /// <value>The transaction config resource.</value>
        public CommerceResource TransactionConfigResource
        {
            get
            {
                CommerceResource commerceResource = CommerceResourceCollection[KeyNames.TransactionConfig.ResourceKey] as CommerceResource;
                return commerceResource;
            }
        }

        /// <summary>
        /// Gets the transaction config connection string.
        /// </summary>
        /// <value>The transaction config connection string.</value>
        public string TransactionConfigConnectionString
        {
            get { return (this.TransactionConfigResource != null ? this.TransactionConfigResource[KeyNames.TransactionConfig.ConnectionString] as string : null);  }
        }

        /// <summary>
        /// Gets the transaction config SQL connection string.
        /// </summary>
        /// <value>The transaction config SQL connection string.</value>
        public string TransactionConfigSqlConnectionString
        {
            get
            {
                if (transactionConfigSqlConnectionString == null)
                {
                    transactionConfigSqlConnectionString = TransactionConfigConnectionString;
                    if (transactionConfigSqlConnectionString != null)
                    {
                        transactionConfigSqlConnectionString = Helper.ConvertOdbcToSqlConnectionString(transactionConfigSqlConnectionString);
                    }
                }
                return transactionConfigSqlConnectionString;
            }
        }
        #endregion
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="CommerceResources"/> class.
        /// </summary>
        /// <param name="commerceResourceCollection">The commerce resource collection.</param>
        public CommerceResources(CommerceResourceCollection commerceResourceCollection)
        {
            Helper.CheckForNullReference(commerceResourceCollection, "commerceResourceCollection");

            this.commerceResourceCollection = commerceResourceCollection;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CommerceResources"/> class.
        /// </summary>
        /// <param name="siteName">The name of the site.</param>
        public CommerceResources(string siteName)
        {
            Helper.CheckEmptyString(siteName, "siteName");

            this.commerceResourceCollection = new CommerceResourceCollection(siteName);
        }
        #endregion

        #region Nested Types
        /// <summary>
        /// Keys used to access the properties in the dictionary.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes")]
        public struct KeyNames
        {
            /// <summary>
            /// Represents the key values for the Inventory Resource dictionary used by Commerce Server.
            /// </summary>
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
            public struct Inventory
            {
                /// <remarks />
                public const string ResourceKey = "Inventory";
                /// <remarks />
                public const string ConnectionString = "connstr_db_inventory";
            }

            /// <summary>
            /// Represents the key values for the Transaction Config Resource dictionary used by Commerce Server.
            /// </summary>
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
            public struct TransactionConfig
            {
                /// <remarks />
                public const string ResourceKey = "Transaction Config";
                /// <remarks />
                public const string ConnectionString = "connstr_db_TransactionConfig";
            }

            /// <summary>
            /// Represents the key values for the Marketing Resource dictionary used by Commerce Server.
            /// </summary>
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
            public struct Marketing
            {
                /// <remarks />
                public const string ResourceKey = "Marketing";
                /// <remarks />
                public const string ConnectionString = "connstr_db_marketing";
                /// <remarks />
                public const string DefaultMarketingSystemLanguage = "s_DefaultMarketingSystemLanguage";
            }
        }
        #endregion
    }
}